using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace LogAnalyzer.Grabbers.Dialogs
{
    public partial class SerialPortGrabberCfg : Form
    {
       
        public SerialPortGrabberCfg()
        {
            InitializeComponent();

            Text = Globalization.ResourceManager.Current.GetString("SERIAL_PORT_GRABBER_CONFIGURATION");
            grpConf.Text = Globalization.ResourceManager.Current.GetString("SERIAL_PORT");

            InitCombos();
        }


        public string PortName
        {
            get { return cmbPortName.SelectedItem.ToString() ; }
            set 
            {
                for (int i = 0; i < cmbPortName.Items.Count; i++)
                {
                    if (cmbPortName.Items[i].ToString().Equals(value))
                    {
                        cmbPortName.SelectedIndex = i;
                        break;
                    }
                } 
            }
        }
        public int PortBaudRate
        {
            get { return int.Parse(cmbPortBaudRate.SelectedItem.ToString()); }
            set
            {
                for (int i = 0; i < cmbPortBaudRate.Items.Count; i++)
                {
                    if (cmbPortBaudRate.Items[i].ToString().Equals(value.ToString()))
                    {
                        cmbPortBaudRate.SelectedIndex = i;
                        break;
                    }
                } 
            }
        }
        public Parity Parity
        {
            get 
            {
                return (Parity)Enum.Parse(typeof(Parity), cmbParity.SelectedItem.ToString());
            }
            set
            {
                string[] names = Enum.GetNames(typeof(Parity));
                for (int i = 0; i < names.Length; i++)
                {
                    if (names[i].Equals(Enum.GetName(typeof(Parity), value)))
                    {
                        cmbParity.SelectedIndex = i;
                        return;
                    }
                }
            }
        }
        public StopBits StopBits
        {
            get
            {
                return (StopBits)Enum.Parse(typeof(StopBits), cmbStopBits.SelectedItem.ToString());
            }
            set
            {
                string[] names = Enum.GetNames(typeof(StopBits));
                for (int i = 0; i < names.Length; i++)
                {
                    if (names[i].Equals(Enum.GetName(typeof(StopBits), value)))
                    {
                        cmbStopBits.SelectedIndex = i;
                        return;
                    }
                }
            }
        }
        public bool DTREnabled
        {
            get
            {
                return chkDtr.Checked;
            }
            set
            {
                chkDtr.Checked = value;
            }
        }
        public bool RTSEnabled
        {
            get
            {
                return chkRts.Checked;
            }
            set
            {
                chkRts.Checked = value;
            }
        }
        public int DataBits
        {
            get { return (int)nudDataBits.Value; }
            set { nudDataBits.Value = value; }
        }
        public DataEncoding Encoding
        {
            get
            {
                return (DataEncoding)Enum.Parse(typeof(DataEncoding), cmbEncoding.SelectedItem.ToString());
            }
            set
            {
                string[] names = Enum.GetNames(typeof(DataEncoding));
                for (int i = 0; i < names.Length; i++)
                {
                    if (names[i].Equals(Enum.GetName(typeof(DataEncoding), value)))
                    {
                        cmbEncoding.SelectedIndex = i;
                        return;
                    }
                }
            }
        }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (cmbPortName.SelectedIndex > -1
                && cmbPortBaudRate.SelectedIndex > -1)
            {
                DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("Select a port name and its baud rate.", Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void InitCombos()
        {
            string[] ports = System.IO.Ports.SerialPort.GetPortNames();

            foreach (string port in ports)
            {
                cmbPortName.Items.Add(port);
            }

            int[] speeds = new int[] { 300, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200 };

            foreach (int speed in speeds)
            {
                cmbPortBaudRate.Items.Add(speed);
            }

            foreach (string name in Enum.GetNames(typeof(System.IO.Ports.Parity)))
            {
                cmbParity.Items.Add(name);
            }

            foreach (string name in Enum.GetNames(typeof(System.IO.Ports.StopBits)))
            {
                cmbStopBits.Items.Add(name);
            }

            foreach (string name in Enum.GetNames(typeof(DataEncoding)))
            {
                cmbEncoding.Items.Add(name);
            }
        }


    }
}